package com.example.travelservlet.food.controller;

import com.example.travelservlet.entity.Food;
import com.example.travelservlet.food.service.FoodService;
import com.example.travelservlet.scenery.service.SceneryService;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.user.exception.BizException;
import com.example.travelservlet.utils.ResultUtil;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hxy
 * @date 2022/11/6 16:44
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodService foodService;
    @Autowired
    private SceneryService sceneryService;

    @GetMapping("/findFood/{sceneryIds}")
    public Result<List<Food>> findFood(@PathVariable List<String> sceneryIds,
                                       @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                                       @RequestParam(name = "size",required = false,defaultValue = "10") Integer size){

        if (page < 1){
            throw new BizException("[查询所有食品]:当前页码不能小于1");
        }
        Pageable pageable = PageRequest.of(page-1,size);
        Page pageResult = foodService.findFoodsBySceneryIds(sceneryIds,pageable);
        return ResultUtil.success(pageResult);
    }
}
